package router

import (
	"cabinet/api"
	"cabinet/api/user/v1"
	"cabinet/controllers"
	"cabinet/middleware"
	"cabinet/util"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

// NewRouter 路由配置
func NewRouter() *gin.Engine {
	r := gin.Default()

	// 中间件, 顺序不能改
	r.Use(middleware.Cors())

	// 路由
	v1Api := r.Group("/api/v1")
	{
		v1Api.POST("ping", api.Ping)

		// 用户登录
		v1Api.POST("user/login", v1.UserLogin)

		// 需要登录保护的
		auth := v1Api.Group("")
		auth.Use(middleware.JwtAuthMiddleware())
		{
			// User Routing
			auth.GET("user/me", v1.UserMe)
			auth.DELETE("user/logout", v1.UserLogout)
			// 用户刷新token
			auth.POST("user/refresh", v1.UserTokenRefresh)
		}
	}
	return r
}

func SetupRouter(db *gorm.DB, mqttClient *util.MQTTClient) *gin.Engine {
	r := gin.Default()

	// 创建控制器实例
	cabinetController := &controllers.CabinetController{
		DB:         db,
		MQTTClient: mqttClient,
	}

	// 柜子相关路由
	cabinet := r.Group("/api/cabinet")
	{
		cabinet.POST("/store", cabinetController.Store)   // 存物
		cabinet.POST("/pickup", cabinetController.Pickup) // 取物
	}

	return r
}
